*Create figures*

*Load and append results datasets*
clear
use "Results\fig_B1\results_ambition.dta" 

gen type="ambition"

frame create temp
frame change temp

use "Results\fig_B1\results_educ.dta" 

gen type="educ"

frame create temp2
frame change temp2

use "Results\fig_B1\results_field.dta" 

gen type="field"

frame change default

frameappend temp
frameappend temp2

frame drop temp temp2

*Fig B5a
twoway line s_1_1 aar if type=="educ", lpattern(solid) lcolor(navy) || ///
line s_2_2 aar if type=="educ", lpattern(dash) lcolor(maroon) || ///
line s_3_3 aar if type=="educ", lpattern(shortdash) lcolor(dkorange) || ///
line s_4_4 aar if type=="educ", lpattern(longdash) lcolor(teal) ///
xtitle("year") ytitle("degree of assortative mating") ///
legend(rows(2) cols(2) order(1 "Primary" 2 "Secondary" 3 "Bachelor" 4 "Master & PhD") position(6)) ///
graphregion(fcolor(white)) yscale(range(1 16)) ylabel(1(3)15)

*Fig B5c
twoway line s_1_1 aar if type=="ambition", lpattern(solid) lcolor(teal) || ///
line s_2_2 aar if type=="ambition", lpattern(longdash) lcolor(navy)  || ///
line s_3_3 aar if type=="ambition", lpattern(shortdash) lcolor(dkorange) || ///
line s_4_4 aar if type=="ambition", lpattern(dash) lcolor(maroon) ///
xtitle("year") ytitle("degree of assortative mating") ///
legend(rows(2) cols(2) order(1 "(low w{sub:0}, low g)" 3 "(low w{sub:0}, high g)" 2 "(high w{sub:0}, low g)" 4 "(high w{sub:0}, high g)") position(6)) ///
graphregion(fcolor(white)) yscale(range(1 8)) ylabel(1(2)8)

*Fig B5e
twoway line s_3_3 aar if type=="field", lpattern(solid) lcolor(teal) || ///
line s_4_4 aar if type=="field", lpattern(dash) lcolor(maroon) || ///
line s_5_5 aar if type=="field", lpattern(shortdash) lcolor(lavender) || ///
line s_6_6 aar if type=="field", lpattern(longdash) lcolor(navy) || ///
line s_7_7 aar if type=="field", lpattern(dash_dot) lcolor(dkorange) ///
xtitle("year") ytitle("degree of assortative mating") ///
legend(order(1 "Humanities" 5 "Health" 2 "Social Science" 4 "STEM" 3 "Business") cols(4) rows(2) position(6)) ///
graphregion(fcolor(white)) yscale(range(1 31)) ylabel(1(6)31)


*with lambda=0 all years

forvalues i=1(1)8{
gen S_`i'_`i'_l0=s_`i'_`i'*(0*male_`i'+(1-0)*female_`i')
}

*Fig B5b
twoway line S_1_1_l0 aar if type=="educ", lpattern(solid) lcolor(navy) || ///
line S_2_2_l0 aar if type=="educ", lpattern(dash) lcolor(maroon) || ///
line S_3_3_l0 aar if type=="educ", lpattern(shortdash) lcolor(dkorange) || ///
line S_4_4_l0 aar if type=="educ", lpattern(longdash) lcolor(teal) ///
xtitle("year") ytitle("degree of assortative mating (weighted)") ///
legend(rows(2) cols(2) order(1 "Primary" 2 "Secondary" 3 "Bachelor" 4 "Master & PhD") position(6)) ///
graphregion(fcolor(white)) yscale(range(0 1)) ylabel(0(0.1)1)

*Fig B5d
twoway line S_1_1_l0 aar if type=="ambition", lpattern(solid) lcolor(teal) || ///
line S_2_2_l0 aar if type=="ambition", lpattern(longdash) lcolor(navy)  || ///
line S_3_3_l0 aar if type=="ambition", lpattern(shortdash) lcolor(dkorange) || ///
line S_4_4_l0 aar if type=="ambition", lpattern(dash) lcolor(maroon) ///
xtitle("year") ytitle("degree of assortative mating (weighted)") ///
legend(rows(2) cols(2) order(1 "(low w{sub:0}, low g)" 3 "(low w{sub:0}, high g)" 2 "(high w{sub:0}, low g)" 4 "(high w{sub:0}, high g)") position(6)) ///
graphregion(fcolor(white)) yscale(range(0 1)) ylabel(0(0.1)1)

*Fig B5f
twoway line S_3_3_l0 aar if type=="field", lpattern(solid) lcolor(teal) || ///
line S_4_4_l0 aar if type=="field", lpattern(dash) lcolor(maroon) || ///
line S_5_5_l0 aar if type=="field", lpattern(shortdash) lcolor(lavender) || ///
line S_6_6_l0 aar if type=="field", lpattern(longdash) lcolor(navy) || ///
line S_7_7_l0 aar if type=="field", lpattern(dash_dot) lcolor(dkorange) ///
xtitle("year") ytitle("degree of assortative mating (weighted)") ///
legend(order(1 "Humanities" 5 "Health" 2 "Social Science" 4 "STEM" 3 "Business") cols(4) rows(2) position(6)) ///
graphregion(fcolor(white)) yscale(range(0 1)) ylabel(0(0.1)1)


*Fig B2a
*educ - men*
gen temp=male_1 if type=="educ"
gen temp2=male_1+male_2 if type=="educ"
gen temp3=male_1+male_2+male_3 if type=="educ"
gen temp4=male_1+male_2+male_3+male_4 if type=="educ"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "Primary" 3 "Secondary" 2 "Bachelor" 1 "Master & PhD") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(teal dkorange maroon navy) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B2b
*educ - women*
gen temp=female_1 if type=="educ"
gen temp2=female_1+female_2 if type=="educ"
gen temp3=female_1+female_2+female_3 if type=="educ"
gen temp4=female_1+female_2+female_3+female_4 if type=="educ"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "Primary" 3 "Secondary" 2 "Bachelor" 1 "Master & PhD") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(teal dkorange maroon navy) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B2c
*ambition - men*
gen temp=male_1 if type=="ambition"
gen temp2=male_1+male_3 if type=="ambition"
gen temp3=male_1+male_3+male_2 if type=="ambition"
gen temp4=male_1+male_3+male_2+male_4 if type=="ambition"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "(low w{sub:0}, low g)" 3 "(low w{sub:0}, high g)" 2 "(high w{sub:0}, low g)" 1 "(high w{sub:0}, high g)") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(maroon navy dkorange teal) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B2d
*ambition - women*
gen temp=female_1 if type=="ambition"
gen temp2=female_1+female_3 if type=="ambition"
gen temp3=female_1+female_3+female_2 if type=="ambition"
gen temp4=female_1+female_3+female_2+female_4 if type=="ambition"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "(low w{sub:0}, low g)" 3 "(low w{sub:0}, high g)" 2 "(high w{sub:0}, low g)" 1 "(high w{sub:0}, high g)") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(maroon navy dkorange teal) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B2e
*field - men*
gen temp1=male_1 if type=="field"
gen temp2=male_1+male_2 if type=="field"
gen temp3=male_1+male_2+male_3 if type=="field"
gen temp4=male_1+male_2+male_3+male_7 if type=="field"
gen temp5=male_1+male_2+male_3+male_7+male_4 if type=="field"
gen temp6=male_1+male_2+male_3+male_7+male_4+male_6 if type=="field"
gen temp7=male_1+male_2+male_3+male_7+male_4+male_6+male_5 if type=="field"
gen temp8=male_1+male_2+male_3+male_7+male_4+male_6+male_5+male_8 if type=="field"


twoway area temp8 temp7 temp6 temp5 temp4 temp3 temp2 temp1 aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(8 "Primary" 7 "Secondary" 6 "Humanities" 5 "Health" 4 "Social Science" 3 "STEM" 2 "Business" 1 "Other") cols(4) rows(2) position(6)) ///
yscale(range(0 1)) color(gs12 lavender navy maroon dkorange teal gs12 gs12) graphregion(fcolor(white))

drop temp1 temp2 temp3 temp4 temp5 temp6 temp7 temp8

*Fig B2f
*field - women*
gen temp1=female_1 if type=="field"
gen temp2=female_1+female_2 if type=="field"
gen temp3=female_1+female_2+female_3 if type=="field"
gen temp4=female_1+female_2+female_3+female_7 if type=="field"
gen temp5=female_1+female_2+female_3+female_7+female_4 if type=="field"
gen temp6=female_1+female_2+female_3+female_7+female_4+female_6 if type=="field"
gen temp7=female_1+female_2+female_3+female_7+female_4+female_6+female_5 if type=="field"
gen temp8=female_1+female_2+female_3+female_7+female_4+female_6+female_5+female_8 if type=="field"


twoway area temp8 temp7 temp6 temp5 temp4 temp3 temp2 temp1 aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(8 "Primary" 7 "Secondary" 6 "Humanities" 5 "Health" 4 "Social Science" 3 "STEM" 2 "Business" 1 "Other") cols(4) rows(2) position(6)) ///
yscale(range(0 1)) color(gs12 lavender navy maroon dkorange teal gs12 gs12) graphregion(fcolor(white))

drop temp1 temp2 temp3 temp4 temp5 temp6 temp7 temp8

*Population marginal versions*

drop male_1 male_2 male_3 male_4 male_5 male_6 male_7 male_8 female_1 female_2 female_3 female_4 female_5 female_6 female_7 female_8

forvalues i=1(1)8{
gen male_`i'=.
gen female_`i'=.
}

forvalues i=1(1)4{
replace male_`i'=(male_`i'_marr+male_`i'_sing)/(male_1_marr+male_2_marr+male_3_marr+male_4_marr+male_1_sing+male_2_sing+male_3_sing+male_4_sing) if (type=="educ" |type=="ambition")
replace female_`i'=(female_`i'_marr+female_`i'_sing)/(female_1_marr+female_2_marr+female_3_marr+female_4_marr+female_1_sing+female_2_sing+female_3_sing+female_4_sing) if (type=="educ" |type=="ambition")
}

forvalues i=1(1)8{
replace male_`i'=(male_`i'_marr+male_`i'_sing)/(male_1_marr+male_2_marr+male_3_marr+male_4_marr+male_5_marr+male_6_marr+male_7_marr+male_8_marr+male_1_sing+male_2_sing+male_3_sing+male_4_sing+male_5_sing+male_6_sing+male_7_sing+male_8_sing) if type=="field"
replace female_`i'=(female_`i'_marr+female_`i'_sing)/(female_1_marr+female_2_marr+female_3_marr+female_4_marr+female_5_marr+female_6_marr+female_7_marr+female_8_marr+female_1_sing+female_2_sing+female_3_sing+female_4_sing+female_5_sing+female_6_sing+female_7_sing+female_8_sing) if type=="field"
}

*Fig B1a
*educ - men*
gen temp=male_1 if type=="educ"
gen temp2=male_1+male_2 if type=="educ"
gen temp3=male_1+male_2+male_3 if type=="educ"
gen temp4=male_1+male_2+male_3+male_4 if type=="educ"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "Primary" 3 "Secondary" 2 "Bachelor" 1 "Master & PhD") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(teal dkorange maroon navy) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B1b
*educ - women*
gen temp=female_1 if type=="educ"
gen temp2=female_1+female_2 if type=="educ"
gen temp3=female_1+female_2+female_3 if type=="educ"
gen temp4=female_1+female_2+female_3+female_4 if type=="educ"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "Primary" 3 "Secondary" 2 "Bachelor" 1 "Master & PhD") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(teal dkorange maroon navy) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B1c
*ambition - men*
gen temp=male_1 if type=="ambition"
gen temp2=male_1+male_3 if type=="ambition"
gen temp3=male_1+male_3+male_2 if type=="ambition"
gen temp4=male_1+male_3+male_2+male_4 if type=="ambition"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "(low w{sub:0}, low g)" 3 "(low w{sub:0}, high g)" 2 "(high w{sub:0}, low g)" 1 "(high w{sub:0}, high g)") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(maroon navy dkorange teal) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B1d
*ambition - women*
gen temp=female_1 if type=="ambition"
gen temp2=female_1+female_3 if type=="ambition"
gen temp3=female_1+female_3+female_2 if type=="ambition"
gen temp4=female_1+female_3+female_2+female_4 if type=="ambition"

twoway area temp4 temp3 temp2 temp aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(4 "(low w{sub:0}, low g)" 3 "(low w{sub:0}, high g)" 2 "(high w{sub:0}, low g)" 1 "(high w{sub:0}, high g)") cols(2) rows(2) position(6)) ///
yscale(range(0 1)) color(maroon navy dkorange teal) graphregion(fcolor(white))

drop temp temp2 temp3 temp4

*Fig B1e
*field - men*
gen temp1=male_1 if type=="field"
gen temp2=male_1+male_2 if type=="field"
gen temp3=male_1+male_2+male_3 if type=="field"
gen temp4=male_1+male_2+male_3+male_7 if type=="field"
gen temp5=male_1+male_2+male_3+male_7+male_4 if type=="field"
gen temp6=male_1+male_2+male_3+male_7+male_4+male_6 if type=="field"
gen temp7=male_1+male_2+male_3+male_7+male_4+male_6+male_5 if type=="field"
gen temp8=male_1+male_2+male_3+male_7+male_4+male_6+male_5+male_8 if type=="field"


twoway area temp8 temp7 temp6 temp5 temp4 temp3 temp2 temp1 aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(8 "Primary" 7 "Secondary" 6 "Humanities" 5 "Health" 4 "Social Science" 3 "STEM" 2 "Business" 1 "Other") cols(4) rows(2) position(6)) ///
yscale(range(0 1)) color(gs12 lavender navy maroon dkorange teal gs12 gs12) graphregion(fcolor(white))

drop temp1 temp2 temp3 temp4 temp5 temp6 temp7 temp8

*Fig B1f
*field - women*
gen temp1=female_1 if type=="field"
gen temp2=female_1+female_2 if type=="field"
gen temp3=female_1+female_2+female_3 if type=="field"
gen temp4=female_1+female_2+female_3+female_7 if type=="field"
gen temp5=female_1+female_2+female_3+female_7+female_4 if type=="field"
gen temp6=female_1+female_2+female_3+female_7+female_4+female_6 if type=="field"
gen temp7=female_1+female_2+female_3+female_7+female_4+female_6+female_5 if type=="field"
gen temp8=female_1+female_2+female_3+female_7+female_4+female_6+female_5+female_8 if type=="field"


twoway area temp8 temp7 temp6 temp5 temp4 temp3 temp2 temp1 aar, ///
xtitle("year") ytitle("marginal distribution") ///
legend(order(8 "Primary" 7 "Secondary" 6 "Humanities" 5 "Health" 4 "Social Science" 3 "STEM" 2 "Business" 1 "Other") cols(4) rows(2) position(6)) ///
yscale(range(0 1)) color(gs12 lavender navy maroon dkorange teal gs12 gs12) graphregion(fcolor(white))

drop temp1 temp2 temp3 temp4 temp5 temp6 temp7 temp8